{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gsamllavanav.cityreferobject import get_city_refer_objects, filter_objects_with_description\n",
    "\n",
    "objects = filter_objects_with_description(get_city_refer_objects())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "maps: 100%|██████████| 34/34 [00:00<00:00, 320.21it/s]\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "from tqdm import tqdm\n",
    "\n",
    "object_names = defaultdict(set)\n",
    "\n",
    "for map_name, single_map_objects in tqdm(objects.items(), desc='maps', position=0):\n",
    "    for obj_id, obj in tqdm(single_map_objects.items(), desc='objects', position=1, leave=False):\n",
    "        for desc_id, desc in enumerate(obj.processed_descriptions):\n",
    "            object_names[map_name].add(desc.target)\n",
    "            object_names[map_name].update(desc.surroundings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "maps: 100%|██████████| 34/34 [22:17:06<00:00, 2359.61s/it]   \n"
     ]
    }
   ],
   "source": [
    "\n",
    "import warnings\n",
    "import numpy as np\n",
    "from gsamllavanav.observation import cropclient\n",
    "from gsamllavanav.maps.gsam_map import GSamMap, GSamParams\n",
    "from gsamllavanav.subblocks import _split_map\n",
    "from gsamllavanav.defaultpaths import GSAM_MAPS_DIR\n",
    "\n",
    "gsam_maps_by_map = defaultdict(dict)\n",
    "cropclient.load_image_cache()\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "ALTITUDE = 50\n",
    "MAP_SIZE = 240\n",
    "MAP_METERS = 410\n",
    "\n",
    "dirname = GSAM_MAPS_DIR/f\"full_scan/{(ALTITUDE, MAP_SIZE, MAP_METERS)}\"\n",
    "dirname.mkdir(exist_ok=True)\n",
    "\n",
    "def gsam_scan_map(map_name: str, caption: str):\n",
    "    gsam_map = GSamMap(map_name, (MAP_SIZE, MAP_SIZE), MAP_SIZE/MAP_METERS, [caption], GSamParams(True, 0.2))\n",
    "\n",
    "    if not caption:\n",
    "        return gsam_map.to_array(np.float16)\n",
    "\n",
    "    for pose in _split_map(map_name, ALTITUDE):\n",
    "        rgb = cropclient.crop_image(map_name, pose, (ALTITUDE*10, ALTITUDE*10), 'rgb')\n",
    "        if (rgb > 0).mean() > 0.15:\n",
    "            gsam_map.update_observation(pose, rgb)\n",
    "    return gsam_map.to_array(np.float16)\n",
    "\n",
    "for map_name, single_map_names in tqdm(object_names.items(), desc='maps', position=0):\n",
    "    for caption in tqdm(single_map_names, desc='names', position=1, leave=False):\n",
    "        caption : str = caption.replace('/', ' ')\n",
    "        if (filename := dirname/f\"{map_name}-{caption}.npy\").exists():\n",
    "            continue\n",
    "        np.save(filename, gsam_scan_map(map_name, caption))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gsamllavanav",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
